/**
 * Created with IntelliJ IDEA.
 * User: Lorenzo
 * Date: 15-5-27
 * Time: 下午7:38
 * To change this template use File | Settings | File Templates.
 */


var savePurchaseGroup = function() {
    this.purchaseGroupModel.init();
    alert(JSON.stringify(this.purchaseGroupModel));
    alert(getkey("purchase.group.code"));
    alert(Math.uuid(8,16));
    alert(Math.uuidFast());
    alert(Math.uuidCompact());

    alert(getUuid());
    alert(getUu32id());
    alert(getPKUUID());
    $.ajax({
        url:'/cm/purchaseGroup/savePurchaseGroup',
        type:'POST',
        contentType: 'application/json',
        dataType: "json",
        data:JSON.stringify(this.purchaseGroupModel),
        success:function(data) {
//            alert(data["purchase_name"]);
//            alert(data["purchase_products"]);
//        	  alert(data.purchateam_name);
        	alert(data.purchateam_code);
        },
        error:function() {
            $('#errorMessage').html('服务器内部异常，请重新保存');
        },
        timeout: function () {
            alert("timeout");
        }
    })
}

var renderPersonView = function(purchaseGroupModel) {
    var purchaseHtml = "<tr><td style='line-height:40px'>序号</td><td>采购员编号</td><td>采购员名字</td><td>联系方式</td><td>编辑操作</td></tr>";
    if(purchaseGroupModel != null && purchaseGroupModel.purchase_persos != null && purchaseGroupModel.purchase_persos.length>0) {
        var purchase_persons = purchaseGroupModel.purchase_persos;
        for(var i=0; i<purchase_persons.length; i++) {
            purchaseHtml += "<tr><td style='line-height:40px'>"+(i+1)+"</td><td>";
            if(purchase_persons[i].isEdit==true)
                purchaseHtml += "<input type='text' class='input-small purchaser_code' value='"+purchase_persons[i].purchaser_code+"'></td>" +
                    "<td><input type='text' class='input-small purchaser_name' value='"+purchase_persons[i].purchaser_name+"'></td>" +
                    "<td><input type='text' class='input-small purchaser_phone' value='"+purchase_persons[i].purchaser_phone+"'>" +
                    "<input type='text' style='display: none' value='"+purchase_persons[i].purchaseper_id+"'></td>" +
                    "<td><a href='#' class='memberSaveClass' onclick='saveMember(this)' ></a>"+
                    "<a href='#' class='memberDelClass' onclick='resetMember()'></a></td></tr>";
            else
                purchaseHtml += purchase_persons[i].purchaser_code+"</td>" +
                    "<td>"+purchase_persons[i].purchaser_name+"</td>" +
                    "<td>"+purchase_persons[i].purchaser_phone+"<input type='text' style='display: none' value='"+purchase_persons[i].purchaseper_id+"'></td>" +
                    "<td><a href='#' class='memberEditClass' onclick='editMember(this)' ></a>" +
                    "<a href='#' class='memberDelClass' onclick='deleteMember(this)'></a></td></tr>";

        }
    }
    $('#metaria table').html(purchaseHtml);

}

var addPersonRows = function() {
    var purchasers = this.purchaseGroupModel.purchase_persos;
    for(var i=0; i<purchasers.length; i++) {
        if(purchasers[i].isEdit==true){
            $('#errorMessage').html('请先保存之前的操作');
            return;
        }
    }
    $('#errorMessage').html('');
    var aa =  {
        purchaseper_id:"",
        purchateam_code:"",
        purchaser_code:"",
        purchaser_name:"",
        purchaser_phone:"",
        purchaser_note:"",
        CreateUser:"",
        CreateDate:"",
        WriteUser:"",
        WriteDate:"",
        isEdit: true,
        flag: 1//0:明细,1:新增,2:编辑
    };
    this.purchaseGroupModel.purchase_persos.push(aa);
    renderPersonView(this.purchaseGroupModel);

}

var saveMember = function(arg) {
    var inputDom = $(arg).parent().parent().find('input');
    var purchaser_code;
    var purchaser_name;
    var purchaser_phone;
    var purchaser_id;
    inputDom.each(function(index){
        if(index==0) {
            purchaser_code=this.value;
        }else if(index==1) {
            purchaser_name=this.value;
        }else if(index==2) {
            purchaser_phone=this.value;
        }else {
            purchaser_id=this.value;
        }
    })
    var purchasers = this.purchaseGroupModel.purchase_persos;
    for(var i=0; i<purchasers.length; i++) {
        if(purchasers[i].isEdit==true){
            purchasers[i].isEdit=false;
            purchasers[i].purchaseper_id=purchaser_id;
            purchasers[i].purchaser_code=purchaser_code;
            purchasers[i].purchaser_name=purchaser_name;
            purchasers[i].purchaser_phone=purchaser_phone;
        }
    }
    renderPersonView(this.purchaseGroupModel);
}

var resetMember = function(){
    var purchasers = this.purchaseGroupModel.purchase_persos;
    for(var i=0; i<purchasers.length; i++) {
        if(purchasers[i].isEdit==true){
            if(purchasers[i].flag==1) {
                this.purchaseGroupModel.purchase_persos.splice(i,1);
            }else {
                purchasers[i].isEdit==false;
            }
        }
    }
    renderPersonView(this.purchaseGroupModel);
}

var deleteMember = function(arg){
    var inputDom = $(arg).parent().parent().find('input')[0];
    if(inputDom.value.trim() != '') {

    }else {
        var index = $(arg).parent().parent().find('td').eq(0).html();
        this.purchaseGroupModel.purchase_persos.splice(index-1,1);
    }
    renderPersonView(this.purchaseGroupModel);
}

var editMember = function(arg){
    var index = $(arg).parent().parent().find('td').eq(0).html();
    this.purchaseGroupModel.purchase_persos[index-1].isEdit=true;
    var inputDom = $(arg).parent().parent().find('input')[0];
    if(inputDom.value.trim() != '') {
        this.purchaseGroupModel.purchase_persos[index-1].flag=2;
    }
    renderPersonView(this.purchaseGroupModel);
}

var renderProductView = function(purchaseGroupModel) {
    var productsHtml = "<tr><td style='line-height:40px'>序号</td><td>商品编号</td><td>商品名称</td><td>规格型号</td><td>品牌</td><td>编辑操作</td></tr>";
    if(purchaseGroupModel != null && purchaseGroupModel.purchase_products != null && purchaseGroupModel.purchase_products.length>0) {
        var purchase_products = purchaseGroupModel.purchase_products;
        for(var i=0; i<purchase_products.length; i++) {
            productsHtml += "<tr><td style='line-height:40px'>"+(i+1)+"</td><td>";
            if(purchase_products[i].isEdit==true)
                productsHtml += "<input type='text' class='input-small purchaser_code' value='"+purchase_products[i].pro_code+"'></td>" +
                    "<td><input type='text' class='input-small purchaser_name' value='"+purchase_products[i].pro_name+"'></td>" +
                    "<td><input type='text' class='input-small purchaser_phone' value='"+purchase_products[i].pro_norms+"'></td>" +
                    "<td><input type='text' class='input-small purchaser_phone' value='"+purchase_products[i].pro_brand+"'>" +
                    "<input type='text' style='display: none' value='"+purchase_products[i].purchasepro_id+"'></td>" +
                    "<td><a href='#' class='memberSaveClass' onclick='saveProduct(this)' ></a>"+
                    "<a href='#' class='memberDelClass' onclick='resetProduct()'></a></td></tr>";
            else
                productsHtml += purchase_products[i].pro_code+"</td>" +
                    "<td>"+purchase_products[i].pro_name+"</td>" +
                    "<td>"+purchase_products[i].pro_norms+"</td>"+
                    "<td>"+purchase_products[i].pro_brand+"<input type='text' style='display: none' value='"+purchase_products[i].purchasepro_id+"'></td>" +
                    "<td><a href='#' class='memberEditClass' onclick='editProduct(this)' ></a>" +
                    "<a href='#' class='memberDelClass' onclick='deleteProduct(this)'></a></td></tr>";

        }
    }
    $('#purchaseStaff table').html(productsHtml);

}

var addProductRows = function() {
    var products = this.purchaseGroupModel.purchase_products;
    for(var i=0; i<products.length; i++) {
        if(products[i].isEdit==true){
            $('#errorMessage').html('请先保存之前的操作');
            return;
        }
    }
    $('#errorMessage').html('');
    var aa =  {
        purchasepro_id:"",
        purchateam_code:"",
        pro_code:"",
        pro_name:"",
        pro_norms:"",
        pro_brand:"",
        note:"",
        CreateUser:"",
        CreateDate:"",
        WriteUser:"",
        WriteDate:"",
        isEdit: true,
        flag: 1//0:明细,1:新增,2:编辑
    };
    this.purchaseGroupModel.purchase_products.push(aa);
    renderProductView(this.purchaseGroupModel);

}

var saveProduct = function(arg) {
    var inputDom = $(arg).parent().parent().find('input');
    var pro_code;
    var pro_name;
    var pro_norms;
    var pro_brand;
    var purchasepro_id;
    inputDom.each(function(index){
        if(index==0) {
            pro_code=this.value;
        }else if(index==1) {
            pro_name=this.value;
        }else if(index==2) {
            pro_norms=this.value;
        }else if(index==3){
            pro_brand = this.value;
        }else {
            purchasepro_id=this.value;
        }
    })
    var products = this.purchaseGroupModel.purchase_products;
    for(var i=0; i<products.length; i++) {
        if(products[i].isEdit==true){
            products[i].isEdit=false;
            products[i].purchasepro_id=purchasepro_id;
            products[i].pro_code=pro_code;
            products[i].pro_name=pro_name;
            products[i].pro_norms=pro_norms;
            products[i].pro_brand=pro_brand;
        }
    }
    renderProductView(this.purchaseGroupModel);
}

var resetProduct = function(){
    var products = this.purchaseGroupModel.purchase_products;
    for(var i=0; i<products.length; i++) {
        if(products[i].isEdit==true){
            if(products[i].flag==1) {
                this.purchaseGroupModel.purchase_products.splice(i,1);
            }else {
                products[i].isEdit==false;
            }
        }
    }
    renderProductView(this.purchaseGroupModel);
}

var deleteProduct = function(arg){
    var inputDom = $(arg).parent().parent().find('input')[0];
    if(inputDom.value.trim() != '') {

    }else {
        var index = $(arg).parent().parent().find('td').eq(0).html();
        this.purchaseGroupModel.purchase_products.splice(index-1,1);
    }
    renderProductView(this.purchaseGroupModel);
}

var editProduct = function(arg){
    var index = $(arg).parent().parent().find('td').eq(0).html();
    this.purchaseGroupModel.purchase_products[index-1].isEdit=true;
    var inputDom = $(arg).parent().parent().find('input')[0];
    if(inputDom.value.trim() != '') {
        this.purchaseGroupModel.purchase_products[index-1].flag=2;
    }
    renderProductView(this.purchaseGroupModel);
}

var toggleTab = function(arg) {
    if($(arg).find('span').hasClass('collapseTab')) {
        $(arg).find('#toggleTabText').html('展开&nbsp;');
        $(arg).find('.littleTab').removeClass('collapseTab');
        $(arg).find('.littleTab').addClass('spreedTab');
        $('#baseInfo').hide();
    }else {
        $(arg).find('#toggleTabText').html  ('收起');
        $(arg).find('.littleTab').removeClass('spreedTab');
        $(arg).find('.littleTab').addClass('collapseTab');
        $('#baseInfo').show();
    }
}